LAMP安装php

        为什么把 php 放到最后来安装,是有原因的,因为在编译 php 的时候,有指定 mysql 以及 apache 的路径,如果不先安装好 mysql 和 apache 就没有办法安装 php 。而 apache 和 mysql 的安装顺序就无所谓了。
        php官方下载地址

下载 php :

1
2
[rot@localhost httpd-2.2.16]# cd /usr/local/src
[root@localhost src]# wget http://am1.php.net/distributions/php-5.3.27.tar.gz

解压源码包:

1
[root@localhost src]# tar zxf php-5.3.27.tar.gz

依赖包安装

1
yum install -y bzip2-devel curl-devel db4-devel libjpeg-devel libpng-devel libXpm-devel gmp-devel libc-client-devel openldap-devel unixODBC-devel postgresql-devel sqlite-devel aspell-devel net-snmp-devel libxslt-devel libxml2-devel pcre-devel mysql-devel unixODBC-devel postgresql-devel pspell-devel net-snmp-devel

配置编译参数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
root@localhost src]# cd php-5.3.27
[root@localhost php-5.3.27]# ./configure \
--prefix=/usr/local/php \
--with-apxs2=/usr/local/apache2/bin/apxs \
--with-config-file-path=/usr/local/php/etc \
--with-mysql=/usr/local/mysql \
--with-libxml-dir \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-iconv-dir \
--with-zlib-dir \
--with-bz2 \
--with-openssl \
--with-mcrypt \
--enable-soap \
--enable-gd-native-ttf \
--enable-mbstring \
--enable-sockets \
--enable-exif \
--disable-ipv6

        在这一步,遇到如下错误:

错误1:

1
configure: error: xml2-config not found. Please check your libxml2 installation.

        解决办法:

1
yum install -y libxml2-devel

错误2:

1
configure: error: Cannot find OpenSSL's <evp.h>

        解决办法:

1
yum install -y openssl openssl-devel

错误3:

1
2
checking for BZip2 in default path... not found
configure: error: Please reinstall the BZip2 distribution

        解决办法:

1
yum install -y bzip2 bzip2-devel

错误4:

1
configure: error: png.h not found.

        解决办法:

1
yum install -y libpng libpng-devel

错误5:

1
configure: error: freetype.h not found.

        解决办法:

1
yum install -y freetype freetype-devel

错误6:

1
configure: error: mcrypt.h not found. Please reinstall libmcrypt.

        解决办法:

1
2
yum install -y epel-release
yum install -y libmcrypt-devel

        rpm安装 epel

1
rpm -ivh "http://www.aminglinux.com/bbs/data/attachment/forum/month_1211/epel-release-6-7.noarch.rpm"

        因为centos 默认的 yum 源没有 libmcrypt-devel 这个包,只能借助 epel 的 yum 源。

错误7:

1
configure: error: jpeglib.h not found.

        解决办法:

1
yum -y install libjpeg-devel

        编译:

1
[root@localhost php-5.3.27]# make

        安装:

1
[root@localhost php-5.3.27]# make install

        安装完成后,还不能使用,还需要进一步配置一下。

        拷贝 php 配置文件:

1
[root@localhost php-5.3.27]# cp php.ini-production /usr/local/php/etc/php.ini

        修改 apache 配置文件

1
[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf

        找到:

1
2
3
4
5
6
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
</Directory>

        改为:

1
2
3
4
5
6
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Allow from all
</Directory>

        说明:如果不修改这个地方,访问网站会禁止访问,显示403。

        然后找到:

1
AddType application/x-gzip .gz .tgz

        在该行下添加:

1
AddType application/x-httpd-php .php

        找到:

1
2
3
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>

        将该行改为:

1
2
3
<IfModule dir_module>
DirectoryIndex index.html index.htm index.php
</IfModule>

        说明:增加针对 php 的索引,如果一个站点默认页为 index.php ,那么就得加上这个index.php 的支持。

        再找到:

1
#ServerName www.example.com:80

        修改为:

1
ServerName www.example.com:80

        如果不去掉 # ,则启动 apache 时,会有警告信息 “httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localfomain for SververName”,看起来像是错误,其实没有影响。

        查看配置文件是否有问题:

1
[root@localhost ~]# /usr/local/apache2/bin/apachectl -t

        如果显示 Syntax OK ,说明配置没有问题。然后启动服务:

1
[root@localhost ~]# /usr/local/apache2/bin/apachectl start

        检查 apache 是否正常启动的命令是:

1
2
3
4
5
6
7
8
9
10
11
[root@lamp ~]# ps aux |grep httpd
root 1434 0.0 0.2 32576 10184 ? Ss 14:18 0:00 /usr/local/apache2/bin/httpd -k start
daemon 1435 0.0 0.2 32708 9084 ? S 14:18 0:00 /usr/local/apache2/bin/httpd -k start
daemon 1436 0.0 0.2 32708 10012 ? S 14:18 0:00 /usr/local/apache2/bin/httpd -k start
daemon 1437 0.0 0.2 32708 9084 ? S 14:18 0:00 /usr/local/apache2/bin/httpd -k start
daemon 1438 0.0 0.2 32708 9084 ? S 14:18 0:00 /usr/local/apache2/bin/httpd -k start
daemon 1439 0.0 0.2 32708 9084 ? S 14:18 0:00 /usr/local/apache2/bin/httpd -k start
daemon 1440 0.0 0.2 32708 9084 ? S 14:18 0:00 /usr/local/apache2/bin/httpd -k start
root 1459 0.0 0.0 5980 736 pts/0 R+ 15:56 0:00 grep --color httpd
[root@lamp ~]# netstat -lnp |grep httpd
tcp 0 0 :::80 :::* LISTEN 1434/httpd

        看看有没有进程列表。如果有显示这行,则启动了。 也可以使用curl命令简单测试:

1
2
[root@localhost ~]# curl localhost
<html><body><h1>It works!</h1></body></html>

        只有这样显示才正确。